package com.example.enterprisemanager.mvvm.fragment.manager;

import com.example.enterprisemanager.base.BaseModel;
import com.example.enterprisemanager.mvvm.ben.User;
import com.example.enterprisemanager.mvvm.fragment.manager.ben.EnrollGoods;
import com.example.enterprisemanager.mvvm.fragment.manager.ben.Goods;
import com.example.enterprisemanager.mvvm.fragment.manager.ben.PurchaseGoods;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobBatch;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BatchResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

public class EnrollModel extends BaseModel {
    private Disposable mGetHaveDisposable;
    private Disposable mHaveEnrollDisposable;
    private Disposable mBatchDisposable;
    private Disposable mSaveDisposable;

    public void getHavePurchase(Consumer<List<PurchaseGoods>> success, Consumer<Exception> fail) {
        BmobQuery<PurchaseGoods> getPurchase = new BmobQuery<>();
        mGetHaveDisposable = getPurchase.findObjects(new FindListener<PurchaseGoods>() {
            @Override
            public void done(List<PurchaseGoods> list, BmobException e) {
                try {
                    if (e == null) {
                        success.accept(list);
                    } else {
                        fail.accept(e);
                    }
                } catch (Exception e1) {

                }
            }
        });

    }

    public void haveEnroll(PurchaseGoods purchaseGoods, Action success, Consumer<Exception> fail) {
        mHaveEnrollDisposable = purchaseGoods.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                try {
                    if (e == null) {
                        success.run();

                    } else {
                        fail.accept(e);

                    }
                } catch (Exception e1) {

                }
            }
        });

    }


    public void enrollGoods(PurchaseGoods purchaseGoods, int num, Action success, Consumer<Exception> fail) {
        List<BmobObject> goods = new ArrayList<>();

        for (int i = 0; i < num; i++) {
            Goods good = new Goods();
            good.setName(purchaseGoods.getName());
            good.setAvatar(purchaseGoods.getAvatar());
            good.setCompany(purchaseGoods.getCompany());
            good.setPrice(purchaseGoods.getPrice());
            good.setType(purchaseGoods.getType());
            good.setDepartment(null);
            good.setRuin(false);
            goods.add(good);
        }

        mBatchDisposable = new BmobBatch().insertBatch(goods).doBatch(new QueryListListener<BatchResult>() {
            @Override
            public void done(List<BatchResult> results, BmobException e) {
                if (e == null) {
                    List<String> ids = new ArrayList<>();

                    for (int i = 0; i < results.size(); i++) {
                        BatchResult result = results.get(i);
                        BmobException ex = result.getError();
                        if (ex == null) {
                            ids.add(result.getObjectId());
                        } else {
                            Logger.d("第" + i + "个数据批量添加失败：" + ex.getMessage() + "," + ex.getErrorCode());
                        }
                    }

                    String username = BmobUser.getCurrentUser(User.class).getUsername();

                    addEnroll(new EnrollGoods(purchaseGoods.getName(), purchaseGoods.getCompany()
                            , purchaseGoods.getAvatar(), purchaseGoods.getPrice(), purchaseGoods.getType()
                            , purchaseGoods.getStorageName(), ids, username, num, num, 0), success, fail);

                } else {
                    Logger.d("失败：" + e.getMessage() + "," + e.getErrorCode());
                }

            }
        });

    }

    public void addEnroll(EnrollGoods enrollGoods, Action success, Consumer<Exception> fail) {
        mSaveDisposable = enrollGoods.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                try {
                    if (e == null) {
                        success.run();
                    } else {
                        fail.accept(e);
                    }
                } catch (Exception e1) {

                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mGetHaveDisposable != null) {
            mGetHaveDisposable.dispose();
            mGetHaveDisposable = null;
        }
        if (mHaveEnrollDisposable != null) {
            mHaveEnrollDisposable.dispose();
            mHaveEnrollDisposable = null;
        }
        if (mSaveDisposable != null) {
            mSaveDisposable.dispose();
            mSaveDisposable = null;
        }
        if (mBatchDisposable != null) {
            mBatchDisposable.dispose();
            mBatchDisposable = null;
        }
    }
}
